import { join } from '@utils';
import type { Plugin } from 'rollup';

import type * as d from '../../declarations';
import { USER_INDEX_ENTRY_ID } from './entry-alias-ids';

export const userIndexPlugin = (config: d.ValidatedConfig, compilerCtx: d.CompilerCtx): Plugin => {
  return {
    name: 'userIndexPlugin',

    async resolveId(importee) {
      if (importee === USER_INDEX_ENTRY_ID) {
        const usersIndexJsPath = join(config.srcDir, 'index.ts');
        const hasUserIndex = await compilerCtx.fs.access(usersIndexJsPath);
        if (hasUserIndex) {
          return usersIndexJsPath;
        }
        return importee;
      }
      return null;
    },

    async load(id) {
      if (id === USER_INDEX_ENTRY_ID) {
        return `//! Autogenerated index`;
      }
      return null;
    },
  };
};
